#!/bin/bash

# Restrict command to sudo users
if [ "$EUID" -ne 0 ] ; then
  echo "Root priveleges are needed for this operation"
  exit 1
fi

# If run on supervisor of chassis, trigger remote execution of TSB on all linecards
if [ -f /etc/sonic/chassisdb.conf ]; then
  rexec all -c "sudo TSB chassis"
  echo "Please execute \"rexec all -c 'sudo config save -y'\" to preserve System mode in Normal state after reboot\
 or config reload on all linecards"
  exit 0
fi

# toggle the mux to auto if dualtor
if [[ "$(sonic-cfggen -d -v DEVICE_METADATA.localhost.subtype | tr [:upper:] [:lower:])" == *"dualtor"* ]];
then
  logger -t TSB -p user.info "Toggle all mux mode to auto"
  sudo config mux mode auto all
fi

/usr/bin/TS TSB
if [[ "$(sonic-cfggen -d -v DEVICE_METADATA.localhost.type)" == *"SpineRouter"* ]] ; then
  if [[ "$1" != "chassis" ]] ; then
    echo "Please execute 'sudo config save' to preserve System mode in Normal state after reboot or config reload"    
  fi
else
  echo "Please execute 'sudo config save' to preserve System mode in Normal state after reboot or config reload"
fi
